//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Personalization;
namespace DotNetNuke.UI.Utilities
{
    public class DNNClientAPI
    {
        public enum MinMaxPersistanceType
        {
            None,
            Page,
            Cookie,
            Personalization
        }
        public enum PageCallBackType
        {
            GetPersonalization = 0,
            SetPersonalization = 1
        }
        private static Hashtable m_objEnabledClientPersonalizationKeys = new Hashtable();
        public static void AddBodyOnloadEventHandler(Page objPage, string strJSFunction)
        {
            if (strJSFunction.EndsWith(";") == false)
                strJSFunction += ";";
            DotNetNuke.UI.Utilities.ClientAPI.RegisterClientReference(objPage, ClientAPI.ClientNamespaceReferences.dnn);
            if (DotNetNuke.UI.Utilities.ClientAPI.GetClientVariable(objPage, "__dnn_pageload").IndexOf(strJSFunction) == -1)
                DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(objPage, "__dnn_pageload", strJSFunction, false);
        }
        public static void EnableContainerDragAndDrop(Control objTitle, Control objContainer, int ModuleID)
        {
            if (DotNetNuke.UI.Utilities.ClientAPI.ClientAPIDisabled() == false && DotNetNuke.UI.Utilities.ClientAPI.BrowserSupportsFunctionality(DotNetNuke.UI.Utilities.ClientAPI.ClientFunctionality.Positioning))
            {
                DNNClientAPI.AddBodyOnloadEventHandler(objTitle.Page, "__dnn_enableDragDrop()");
                DotNetNuke.UI.Utilities.ClientAPI.RegisterClientReference(objTitle.Page, DotNetNuke.UI.Utilities.ClientAPI.ClientNamespaceReferences.dnn_dom_positioning);
                DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(objTitle.Page, "__dnn_dragDrop", objContainer.ClientID + " " + objTitle.ClientID + " " + ModuleID.ToString() + ";", false);
                string strPanes = "";
                string strPaneNames = "";
                DotNetNuke.Entities.Portals.PortalSettings objPortalSettings = (DotNetNuke.Entities.Portals.PortalSettings)HttpContext.Current.Items["PortalSettings"];

                Control objCtl;
                foreach (string strPane in objPortalSettings.ActiveTab.Panes)
                {
                    objCtl = DotNetNuke.Common.Globals.FindControlRecursive(objContainer.Parent, strPane);
                    if (objCtl != null)
                        strPanes += objCtl.ClientID + ";";
                    strPaneNames += strPane + ";";
                }
                DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(objTitle.Page, "__dnn_Panes", strPanes, true);
                DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(objTitle.Page, "__dnn_PaneNames", strPaneNames, true);
            }
        }
        public static void EnableMinMax(Control objButton, Control objContent, bool blnDefaultMin, MinMaxPersistanceType ePersistanceType)
        {
            EnableMinMax(objButton, objContent, -1, blnDefaultMin, "", "", ePersistanceType);
        }
        public static void EnableMinMax(Control objButton, Control objContent, int intModuleId, bool blnDefaultMin, MinMaxPersistanceType ePersistanceType)
        {
            EnableMinMax(objButton, objContent, intModuleId, blnDefaultMin, "", "", ePersistanceType);
        }
        public static void EnableMinMax(Control objButton, Control objContent, bool blnDefaultMin, string strMinIconLoc, string strMaxIconLoc, MinMaxPersistanceType ePersistanceType)
        {
            EnableMinMax(objButton, objContent, -1, blnDefaultMin, strMinIconLoc, strMaxIconLoc, ePersistanceType);
        }
        public static void EnableMinMax(Control objButton, Control objContent, int intModuleId, bool blnDefaultMin, string strMinIconLoc, string strMaxIconLoc, MinMaxPersistanceType ePersistanceType)
        {
            EnableMinMax(objButton, objContent, intModuleId, blnDefaultMin, strMinIconLoc, strMaxIconLoc, ePersistanceType, 5);
        }
        public static void EnableMinMax(Control objButton, Control objContent, bool blnDefaultMin, string strMinIconLoc, string strMaxIconLoc, MinMaxPersistanceType ePersistanceType, string strPersonalizationNamingCtr, string strPersonalizationKey)
        {
            EnableMinMax(objButton, objContent, -1, blnDefaultMin, strMinIconLoc, strMaxIconLoc, ePersistanceType, 5, strPersonalizationNamingCtr, strPersonalizationKey);
        }
        public static void EnableMinMax(Control objButton, Control objContent, int intModuleId, bool blnDefaultMin, string strMinIconLoc, string strMaxIconLoc, MinMaxPersistanceType ePersistanceType, int intAnimationFrames)
        {
            EnableMinMax(objButton, objContent, intModuleId, blnDefaultMin, strMinIconLoc, strMaxIconLoc, ePersistanceType, intAnimationFrames, null, null);
        }
        public static void EnableMinMax(Control objButton, Control objContent, int intModuleId, bool blnDefaultMin, string strMinIconLoc, string strMaxIconLoc, MinMaxPersistanceType ePersistanceType, int intAnimationFrames, string strPersonalizationNamingCtr, string strPersonalizationKey)
        {
            if (DotNetNuke.UI.Utilities.ClientAPI.BrowserSupportsFunctionality(DotNetNuke.UI.Utilities.ClientAPI.ClientFunctionality.DHTML))
            {
                DotNetNuke.UI.Utilities.ClientAPI.RegisterClientReference(objButton.Page, DotNetNuke.UI.Utilities.ClientAPI.ClientNamespaceReferences.dnn_dom);
                switch (ePersistanceType)
                {
                    case MinMaxPersistanceType.None:
                        AddAttribute(objButton, "onclick", "if (__dnn_SectionMaxMin(this,  '" + objContent.ClientID + "')) return false;");
                        if (!String.IsNullOrEmpty(strMinIconLoc))
                        {
                            AddAttribute(objButton, "max_icon", strMaxIconLoc);
                            AddAttribute(objButton, "min_icon", strMinIconLoc);
                        }
                        break;
                    case MinMaxPersistanceType.Page:
                        AddAttribute(objButton, "onclick", "if (__dnn_SectionMaxMin(this,  '" + objContent.ClientID + "')) return false;");
                        if (!String.IsNullOrEmpty(strMinIconLoc))
                        {
                            AddAttribute(objButton, "max_icon", strMaxIconLoc);
                            AddAttribute(objButton, "min_icon", strMinIconLoc);
                        }
                        break;
                    case MinMaxPersistanceType.Cookie:
                        if (intModuleId != -1)
                        {
                            AddAttribute(objButton, "onclick", "if (__dnn_ContainerMaxMin_OnClick(this, '" + objContent.ClientID + "')) return false;");
                            DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(objButton.Page, "containerid_" + objContent.ClientID, intModuleId.ToString(), true);
                            DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(objButton.Page, "cookieid_" + objContent.ClientID, "_Module" + intModuleId.ToString() + "_Visible", true);
                            DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(objButton.Page, "min_icon_" + intModuleId.ToString(), strMinIconLoc, true);
                            DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(objButton.Page, "max_icon_" + intModuleId.ToString(), strMaxIconLoc, true);
                            DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(objButton.Page, "max_text", DotNetNuke.Services.Localization.Localization.GetString("Maximize"), true);
                            DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(objButton.Page, "min_text", DotNetNuke.Services.Localization.Localization.GetString("Minimize"), true);
                            if (blnDefaultMin)
                            {
                                DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(objButton.Page, "__dnn_" + intModuleId.ToString() + ":defminimized", "true", true);
                            }
                        }
                        break;
                    case MinMaxPersistanceType.Personalization:
                        AddAttribute(objButton, "userctr", strPersonalizationNamingCtr);
                        AddAttribute(objButton, "userkey", strPersonalizationKey);
                        if (EnableClientPersonalization(strPersonalizationNamingCtr, strPersonalizationKey, objButton.Page))
                        {
                            AddAttribute(objButton, "onclick", "if (__dnn_SectionMaxMin(this,  '" + objContent.ClientID + "')) return false;");
                            if (!String.IsNullOrEmpty(strMinIconLoc))
                            {
                                AddAttribute(objButton, "max_icon", strMaxIconLoc);
                                AddAttribute(objButton, "min_icon", strMinIconLoc);
                            }
                        }
                        break;
                }
            }
            if (MinMaxContentVisibile(objButton, intModuleId, blnDefaultMin, ePersistanceType))
            {
                if (DotNetNuke.UI.Utilities.ClientAPI.BrowserSupportsFunctionality(DotNetNuke.UI.Utilities.ClientAPI.ClientFunctionality.DHTML))
                {
                    AddStyleAttribute(objContent, "display", "");
                }
                else
                {
                    objContent.Visible = true;
                }
                if (!String.IsNullOrEmpty(strMinIconLoc))
                    SetMinMaxProperties(objButton, strMinIconLoc, DotNetNuke.Services.Localization.Localization.GetString("Minimize"), DotNetNuke.Services.Localization.Localization.GetString("Minimize"));
            }
            else
            {
                if (DotNetNuke.UI.Utilities.ClientAPI.BrowserSupportsFunctionality(DotNetNuke.UI.Utilities.ClientAPI.ClientFunctionality.DHTML))
                {
                    AddStyleAttribute(objContent, "display", "none");
                }
                else
                {
                    objContent.Visible = false;
                }
                if (!String.IsNullOrEmpty(strMaxIconLoc))
                    SetMinMaxProperties(objButton, strMaxIconLoc, DotNetNuke.Services.Localization.Localization.GetString("Maximize"), DotNetNuke.Services.Localization.Localization.GetString("Maximize"));
            }
            if (intAnimationFrames != 5)
            {
                DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(objButton.Page, "animf_" + objContent.ClientID, intAnimationFrames.ToString(), true);
            }
        }
        private static void SetMinMaxProperties(Control objButton, string strImage, string strToolTip, string strAltText)
        {
            if (objButton is LinkButton)
            {
                LinkButton objLB = (LinkButton)objButton;
                objLB.ToolTip = strToolTip;
                if (objLB.Controls.Count > 0)
                {
                    SetImageProperties(objLB.Controls[0], strImage, strToolTip, strAltText);
                }
            }
            else if (objButton is System.Web.UI.WebControls.Image)
            {
                SetImageProperties((System.Web.UI.WebControls.Image)objButton, strImage, strToolTip, strAltText);
            }
            else if (objButton is System.Web.UI.WebControls.ImageButton)
            {
                SetImageProperties((System.Web.UI.WebControls.LinkButton)objButton, strImage, strToolTip, strAltText);
            }
        }
        private static void SetImageProperties(Control objControl, string strImage, string strToolTip, string strAltText)
        {
            if (objControl is System.Web.UI.WebControls.Image)
            {
                System.Web.UI.WebControls.Image objImage = (System.Web.UI.WebControls.Image)objControl;
                objImage.ImageUrl = strImage;
                objImage.AlternateText = strAltText;
                objImage.ToolTip = strToolTip;
            }
            else if (objControl is System.Web.UI.WebControls.ImageButton)
            {
                System.Web.UI.WebControls.ImageButton objImage = (System.Web.UI.WebControls.ImageButton)objControl;
                objImage.ImageUrl = strImage;
                objImage.AlternateText = strAltText;
                objImage.ToolTip = strToolTip;
            }
            else if (objControl is System.Web.UI.HtmlControls.HtmlImage)
            {
                System.Web.UI.HtmlControls.HtmlImage objImage = (System.Web.UI.HtmlControls.HtmlImage)objControl;
                objImage.Src = strImage;
                objImage.Alt = strAltText;
            }
        }

        public static bool MinMaxContentVisibile(Control objButton, bool blnDefaultMin, MinMaxPersistanceType ePersistanceType)
        {
            return MinMaxContentVisibile(objButton, -1, blnDefaultMin, ePersistanceType);
        }
        public static void MinMaxContentVisibile(Control objButton, bool blnDefaultMin, MinMaxPersistanceType ePersistanceType, bool value)
        {
            MinMaxContentVisibile(objButton, -1, blnDefaultMin, ePersistanceType, value);
        }

        public static bool MinMaxContentVisibile(Control objButton, int intModuleId, bool blnDefaultMin, MinMaxPersistanceType ePersistanceType)
        {
            if (System.Web.HttpContext.Current != null)
            {
                switch (ePersistanceType)
                {
                    case MinMaxPersistanceType.Page:
                        string sExpanded = DotNetNuke.UI.Utilities.ClientAPI.GetClientVariable(objButton.Page, objButton.ClientID + ":exp");
                        if (!String.IsNullOrEmpty(sExpanded))
                        {
                            return sExpanded == "1" ? true : false;
                        }
                        else
                        {
                            return !blnDefaultMin;
                        }
                    case MinMaxPersistanceType.Cookie:
                        if (intModuleId != -1)
                        {
                            HttpCookie objModuleVisible = System.Web.HttpContext.Current.Request.Cookies["_Module" + intModuleId.ToString() + "_Visible"];
                            if (objModuleVisible != null)
                            {
                                return objModuleVisible.Value != "false";
                            }
                            else
                            {
                                return !blnDefaultMin;
                            }
                        }
                        else
                        {
                            return true;
                        }
                    case MinMaxPersistanceType.Personalization:
                        string strVisible = Convert.ToString(Personalization.GetProfile(Globals.GetAttribute(objButton, "userctr"), Globals.GetAttribute(objButton, "userkey")));
                        if (string.IsNullOrEmpty(strVisible))
                        {
                            return blnDefaultMin;
                        }
                        else
                        {
                            return Convert.ToBoolean(strVisible);
                        }
                    default:
                        return !blnDefaultMin;
                }
            }
            return Null.NullBoolean;
        }

        public static void MinMaxContentVisibile(Control objButton, int intModuleId, bool blnDefaultMin, MinMaxPersistanceType ePersistanceType, bool value)
        {
            if (System.Web.HttpContext.Current != null)
            {
                switch (ePersistanceType)
                {
                    case MinMaxPersistanceType.Page:
                        DotNetNuke.UI.Utilities.ClientAPI.RegisterClientVariable(objButton.Page, objButton.ClientID + ":exp", Convert.ToInt32(value).ToString(), true);
                        break;
                    case MinMaxPersistanceType.Cookie:
                        HttpCookie objModuleVisible =
                            new HttpCookie("_Module" + intModuleId + "_Visible");
                        if (objModuleVisible != null)
                        {
                            objModuleVisible.Value = value.ToString().ToLower();
                            objModuleVisible.Expires = DateTime.MaxValue;
                            System.Web.HttpContext.Current.Response.AppendCookie(objModuleVisible);
                        }
                        break;
                    case MinMaxPersistanceType.Personalization:
                        Personalization.SetProfile(
                            Globals.GetAttribute(objButton, "userctr"),
                            Globals.GetAttribute(objButton, "userkey"),
                            value.ToString());
                        break;
                }
            }
        }

        private static void AddAttribute(Control objControl, string strName, string strValue)
        {
            if (objControl is HtmlControl)
            {
                ((HtmlControl)objControl).Attributes.Add(strName, strValue);
            }
            else if (objControl is WebControl)
            {
                ((WebControl)objControl).Attributes.Add(strName, strValue);
            }
        }
        private static void AddStyleAttribute(Control objControl, string strName, string strValue)
        {
            if (objControl is HtmlControl)
            {
                if (!String.IsNullOrEmpty(strValue))
                {
                    ((HtmlControl)objControl).Style.Add(strName, strValue);
                }
                else
                {
                    ((HtmlControl)objControl).Style.Remove(strName);
                }
            }
            else if (objControl is WebControl)
            {
                if (!String.IsNullOrEmpty(strValue))
                {
                    ((WebControl)objControl).Style.Add(strName, strValue);
                }
                else
                {
                    ((WebControl)objControl).Style.Remove(strName);
                }
            }
        }
        public static bool EnableClientPersonalization(string strNamingContainer, string strKey, Page objPage)
        {
            if (DotNetNuke.UI.Utilities.ClientAPI.BrowserSupportsFunctionality(Utilities.ClientAPI.ClientFunctionality.XMLHTTP))
            {
                ClientAPI.GetCallbackEventReference(objPage, "", "", "", "");
                lock (m_objEnabledClientPersonalizationKeys.SyncRoot)
                {
                    if (IsPersonalizationKeyRegistered(strNamingContainer + ClientAPI.CUSTOM_COLUMN_DELIMITER + strKey) == false)
                    {
                        m_objEnabledClientPersonalizationKeys.Add(strNamingContainer + ClientAPI.CUSTOM_COLUMN_DELIMITER + strKey, "");
                    }
                }
                return true;
            }
            return false;
        }
        public static bool IsPersonalizationKeyRegistered(string strKey)
        {
            return m_objEnabledClientPersonalizationKeys.ContainsKey(strKey);
        }
    }
}
